﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;


namespace Logistics.KDNiao
{
    /// <summary>
    /// 快递鸟SDK
    /// </summary>
    public class KDNiaoSdk
    {
        /// <summary>
        /// 查询订单物流跟踪
        /// </summary>
        /// <returns></returns>
        public static TracesModel GetTraces(TrancesParam model)
        {
            var requestData = JsonConvert.SerializeObject(model);
            var html = Request("http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx", requestData, "1002");

            html = html.Replace("\n", string.Empty);
            //根据公司业务处理返回的信息......
            return JsonConvert.DeserializeObject<TracesModel>(html);
        }

        /// <summary>
        /// 获取常用的快递公司
        /// </summary>
        /// <returns></returns>
        public static IList<Shipper> GetShipperList()
        {
            var list = new List<Shipper>()
            {
                new Shipper("SF","顺丰快递","仅支持通过快递鸟电子面单接口下单返回的顺丰单号查询") ,
                new Shipper("HTKY","百世汇通","仅支持物流跟踪接口 1008、8008 查询") ,
                new Shipper("ZTO","中通快递") ,
                new Shipper("STO","申通快递","仅支持在途监控接口 8001、8008 查询") ,
                new Shipper("YTO","圆通快递") ,
                new Shipper("YD","韵达快递") ,
                new Shipper("YZPY","中国邮政") ,
                new Shipper("EMS","EMS") ,
                new Shipper("HHTT","天天快递") ,
                new Shipper("JD","京东物流") ,
                new Shipper("UC","优速快递") ,
                new Shipper("DBL","德邦物流") ,
                new Shipper("FAST","快捷快递") ,
                new Shipper("ZJS","宅急送") ,
                new Shipper("TNT","TNT快递") ,
                new Shipper("UPS","UPS") ,
                new Shipper("DHL","DHL") ,
                new Shipper("FEDEX","FEDEX联邦(国内件）") ,
                new Shipper("FEDEX_GJ","FEDEX联邦(国际件）") ,

                new Shipper("GTO","国通快递") ,
                new Shipper("SURE","速尔快递") ,
                new Shipper("QFKD","全峰快递")
            };
            return list;
        }

        public static bool IsSuccess(TracesModel json)
        {
            if (json != null && json.Success)
                return true;
            return false;
        }
        static string Request(string url, string requestData, string requestType)
        {
            Dictionary<string, string> param = new Dictionary<string, string>();
            param.Add("RequestData", HttpUtility.UrlEncode(requestData, Encoding.UTF8));
            param.Add("EBusinessID", Config.EBusinessID);
            param.Add("RequestType", requestType);
            string dataSign = Function.Encrypt(requestData, Config.AppKey, "UTF-8");
            param.Add("DataSign", HttpUtility.UrlEncode(dataSign, Encoding.UTF8));
            param.Add("DataType", "2");

            return Function.Request(url, param);
        }
    }
}